import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const Login = () => import('views/login/Login')
const Home = () => import('views/home/Home')
const HomeWelcome = () => import('views/home/homeWelcome/HomeWelcome')
const HomeUser = () => import('views/home/homeUser/HomeUser')
const Roles = () => import('views/purview/roles/Roles')
const List = () => import('views/purview/list/List')
const Goods = () => import('views/goods/goodsSort/Goods')
const GoodsPara = () => import('views/goods/goodsPara/GoodsPara')
const GoodsList = () => import('views/goods/goodsList/GoodsList')
const GoodsAdd = () => import('views/goods/goodsList/GoodsAdd')
const Order = () => import('views/order/Order')
const Report = () => import('views/report/Report')

const routes = [
  {
    path : '/',
    redirect : '/Login'
  },
  {
    path : '/login',
    component : Login
  },
  {
    path : '/home',
    component : Home,
    redirect : '/welcome',
    children : [
      {
        path : '/welcome',
        component : HomeWelcome
      },
      {
        path : '/users',
        component : HomeUser
      },
      {
        path : '/roles',
        component : Roles
      },
      {
        path : '/rights' ,
        component : List
      },
      {
        path : '/categories',
        component : Goods
      },
      {
        path : '/params',
        component : GoodsPara
      },
      {
        path : '/goods',
        component : GoodsList
      },
      {
        path : '/goods/add',
        component : GoodsAdd
      },
      {
        path : '/orders',
        component : Order
      },
      {
        path : '/reports',
        component : Report
      }
    ]
  }
]
const router = new VueRouter({
  mode : 'history',
  base: process.env.BASE_URL,
  routes
})


router.beforeEach((to,from,next) => {
  const tokenStr = window.sessionStorage.getItem('token')

  if(to.path === '/login'){
    return next()
  }
  
  if(!tokenStr){
    return next('/login')
  }else{
    next()
  }

})
export default router